﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Touch2Like.Web.Admin
{
    public partial class Goods : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(WebUtility.Username))
            {
                Response.Redirect("/Admin/Default.aspx");
            }

            if (!IsPostBack)
            {
                Touch2Like.DAL.ProductDAL product = new Touch2Like.DAL.ProductDAL();
                GridView_Goods.DataSource = product.GetAllProducts();
                GridView_Goods.DataBind();
            }
        }

        protected void GridView_Goods_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Goods.PageIndex = e.NewPageIndex;
            Touch2Like.DAL.ProductDAL product = new Touch2Like.DAL.ProductDAL();
            GridView_Goods.DataSource = product.GetAllProducts();
            GridView_Goods.DataBind();
        }

        protected void GridView_Goods_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (Session["SortDirection"] == null) Session["SortDirection"] = "ASC";
            if (Session["SortBy"] == null) Session["SortBy"] = "ListID";
            if (e.SortExpression == Session["SortBy"].ToString()) Session["SortDirection"] = Session["SortDirection"] == "ASC" ? "DESC" : "ASC";
            else Session["SortDirection"] = "ASC";

            Touch2Like.DAL.ProductDAL product = new Touch2Like.DAL.ProductDAL();
            DataTable dt = product.GetAllProducts().Tables[0];
            DataView dv = new DataView(dt);
            Session["SortBy"] = e.SortExpression;
            dv.Sort = GetSortBy();
            GridView_Goods.DataSource = dv;
            GridView_Goods.DataBind();
        }

        string GetSortBy()
        {
            string sortby = "";

            sortby = " ListID";

            if (Session["SortBy"] != null)
            {
                if (!string.IsNullOrEmpty(Session["SortBy"].ToString()))
                {
                    sortby = " " + Session["SortBy"].ToString();
                }
            }
            else Session["SortBy"] = sortby;

            if (Session["SortDirection"] != null)
            {
                if (!string.IsNullOrEmpty(Session["SortDirection"].ToString()))
                {
                    return sortby + " " + Session["SortDirection"].ToString();
                }
            }

            return sortby + " " + "ASC";
        }

        protected void btn_Delete_Click(object sender, EventArgs e)
        {
            Touch2Like.DAL.ProductDAL product = new Touch2Like.DAL.ProductDAL();
            foreach (GridViewRow row in GridView_Goods.Rows)
            {
                CheckBox cb_ListID = row.FindControl("cb_ListID") as CheckBox;
                if (cb_ListID != null && cb_ListID.Checked)
                {
                    HiddenField tb_ListID = row.FindControl("tb_ListID") as HiddenField;
                    if (tb_ListID != null)
                    {
                        if (tb_ListID.Value.Trim() != "")
                            product.DeleteProduct(Convert.ToInt32(tb_ListID.Value.Trim()));
                    }
                }
            }

            DataTable dt = product.GetAllProducts().Tables[0];
            DataView dv = new DataView(dt);
            dv.Sort = GetSortBy();
            GridView_Goods.DataSource = dv;
            GridView_Goods.DataBind();
        }
    }
}